package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.navi.BaiduMapNavigation;
import com.baidu.mapapi.navi.NaviParaOption;
import com.baidu.mapapi.utils.OpenClientUtil;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.WebActivity;
import com.centaline.androidsalesblog.activities.sale.StoreListActivity;
import com.centaline.androidsalesblog.adapter.viewholder.StoreVH;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.model.businissobject.StoreBo;
import com.centaline.androidsalesblog.model.databaseobject.StaffDo;
import com.centaline.androidsalesblog.utils.ContactUtil;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 门店
 * Created by Lihq on 16/2/15.
 */
public class StoreAdapter extends RecyclerView.Adapter<StoreVH> {

    private final ArrayList<StoreBo> list;
    private StoreListActivity storeListActivity;
    private LatLng estLatLng;

    public StoreAdapter(Context context, ArrayList<StoreBo> list) {
        this.storeListActivity = (StoreListActivity) context;
        this.list = list;
    }


    @Override
    public StoreVH onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_store, parent, false);
        return new StoreVH(view);
    }

    @Override
    public void onBindViewHolder(final StoreVH holder, final int position) {
        final StoreBo storeBo = list.get(position);
        holder.atv_store_title.setText(storeBo.getStoreName());
        holder.atv_store_address.setText(storeBo.getStoreAddr());
        holder.atv_agent_count.setText(String.format(Locale.CHINA, "共%d位经纪人为您提供帮助", storeBo.getAgentNum()));
        ArrayList<StaffDo> staffDos = storeBo.getStaffDos();
        if (staffDos != null) {
            StoreAgentAdapter storeAgentAdapter = new StoreAgentAdapter(storeListActivity, GlideLoad.initRound(storeListActivity), staffDos);
            holder.mRecyclerview_horizontal.setAdapter(storeAgentAdapter);
        }
        holder.atv_store_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContactUtil.dial(storeListActivity, storeBo.getStoreTel());
            }
        });
        holder.img_store_arrow.setImageResource(storeBo.isFlag() ? R.drawable.ic_store_arrow_up : R.drawable.ic_store_arrow);
        holder.rl_agent_detail.setVisibility(storeBo.isFlag() ? View.VISIBLE : View.GONE);
        holder.img_360.setVisibility(!TextUtils.isEmpty(storeBo.getTencentVistaUrl())? View.VISIBLE : View.GONE);

        holder.img_360.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                storeListActivity.startActivity(new Intent(storeListActivity, WebActivity.class)
                        .putExtra(ExtraConstant.WebUrl, storeBo.getTencentVistaUrl()));
            }
        });
        holder.rl_store_agent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(position).setFlag(!storeBo.isFlag());
                holder.rl_agent_detail.setVisibility(storeBo.isFlag() ? View.VISIBLE : View.GONE);
                holder.img_store_arrow.setImageResource(storeBo.isFlag() ? R.drawable.ic_store_arrow_up : R.drawable.ic_store_arrow);
//                notifyItemChanged(position);
            }
        });
        holder.atv_store_nav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                estLatLng = new LatLng(storeBo.getLpt_Y(), storeBo.getLpt_X());
                if (storeListActivity.latLng == null || estLatLng == null) {
                    Toast.makeText(storeListActivity, "未获取当前位置", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    NaviParaOption para = new NaviParaOption();
                    para.startPoint(storeListActivity.latLng).endPoint(estLatLng);
                    if (OpenClientUtil.getBaiduMapVersion(storeListActivity) == 0) {
                        BaiduMapNavigation.openWebBaiduMapNavi(para, storeListActivity);
                    } else {
                        BaiduMapNavigation.openBaiduMapNavi(para, storeListActivity);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
